//
//  SQLowOrderTableViewCell.m
//  pai
//
//  Created by pro on 14-12-8.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQLowOrderTableViewCell.h"
#import "ASIHTTPRequest.h"
#import "UIImageView+WebCache.h"
#import "Global.h"

@implementation SQLowOrderTableViewCell

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setItem:(lowCellObject*)auction
{
    //商品图片
    if (self.imageUrl == nil) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/skus/%@/photos.json", IMAGESERVICE,auction.skucode]];
        NSLog(@"get image url:%@",url);
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        
        [request startSynchronous];
        
        NSError *error = [request error];
        
        if (!error) {
            
            NSString *response = [request responseString];
            NSLog(@"%@",response);
            
            NSData *data= [response dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = nil;
            NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            //        NSLog(@"%@",dic);
            //        NSLog(@"home:%@",[dic objectForKey:@"home"]);
            //        NSMutableArray* array =[dic objectForKey:@"intro"];
            //        NSLog(@"count:%d",array.count);
            NSString* imageUrl = [NSString stringWithFormat:@"%@%@",IMAGESERVICE,[dic objectForKey:@"home"]];
            self.imageDic = dic;
            self.imageUrl =imageUrl;
        }
    }
    self.imgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.imgView setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:nil];
    //拍品名称
    self.nameLabel.text = auction.skuname;
    //出价价格
    NSMutableAttributedString* price = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"我的出价：%ld豆",(long)auction.auctprice]];
    [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,5)];
    [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5,[price length]-6)];
    [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange([price length]-1,1)];
    self.contentLabel.attributedText = price;
    NSInteger imagetype = 0;
    NSLog(@"status:%ld",(long)auction.status);
    NSLog(@"hit:%ld",(long)auction.hits);
    NSLog(@"userid:%@",auction.userid);
    switch (auction.status) {
        case 1:
        {
            break;
        }
        case 2:
        {
            if (auction.hits <= 1) {
                imagetype = 1;
            }else{
                imagetype = 2;
            }
            break;
        }
        case 3:
        {
            if ([auction.userid isEqualToString:[Global instance].userinfo.username]&&auction.auctprice == auction.strikeprice) {
                imagetype = 3;
            }else{
                imagetype =2;
            }
            break;
        }
        case 4:
        {
            break;
        }
        case 5:
        {
            break;
        }
        case 9:
        {
            if ([auction.userid isEqualToString:[Global instance].userinfo.username]&&auction.auctprice == auction.strikeprice) {
                imagetype = 3;
            }else{
                imagetype =2;
            }
            break;
        }
        default:
            break;
    }
    switch (imagetype) {
        case 1:
            self.backImage.image = [UIImage imageNamed:@"order_list_wait"];
            break;
        case 2:
            self.backImage.image = [UIImage imageNamed:@"order_list_fail"];
            break;
        case 3:
            self.backImage.image = [UIImage imageNamed:@"order_list_win"];
            break;
        default:
            break;
    }
}
@end

@implementation lowCellObject//刚才.h文件里定义的类在这实现

@synthesize status;
@synthesize sttime;
@synthesize edtime;
@synthesize strikeprice;
@synthesize auction_cnt;
@synthesize userid;
@synthesize skucode;
@synthesize skuname;
@synthesize serialnum;
@synthesize auctprice;
@synthesize created_at;
@synthesize checkflag;
@synthesize hits;
-(id) init
{
    status = 0;
    sttime = 0;
    edtime = 0;
    strikeprice = 0;
    auction_cnt = 0;
    userid = @"";
    skucode = @"";
    skuname = @"";
    serialnum = @"";
    auctprice = 0;
    created_at = 0;
    checkflag = 0;
    hits = 0;
    return self;
};
@end
